import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Sample extends JPanel implements ItemListener, ActionListener
{

    java.util.List <Forex> forexList =new ArrayList <Forex>();
    JComboBox <String> convertFrom =new JComboBox <String>();
    JComboBox <String> convertTo =new JComboBox <String>();

    //iniatialized all the GUI components
    JTextField txtFrom, txtTo;
    JButton compute, exit;
    JLabel from, to;
    JLabel labelFrom, labelTo;
    JPanel mainPanel = new JPanel();
    private JFrame back;
    private JFrame curr;

    double input = 0;
    double result = 0;

    public Sample(JFrame backComp, JFrame curr) {
        this.back = backComp;
        this.curr = curr;
        
        curr.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                back.setVisible(true);
            }
        });
        
        curr.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                back.setVisible(true);
            }
        });
        
        //add all the GUI components
        CurrencyConverter.load();
        setSize(420,300);
        setLayout(null);

        labelFrom = new JLabel("Monetary Unit From:");
        labelFrom.setBounds(30,45,250,30);
        convertFrom = new JComboBox<String>();
        for (Forex f : CurrencyConverter.forexList) {
        	convertFrom.addItem(f.country);
        }
        convertFrom.setBounds(30,70,150,30);

        labelTo = new JLabel("Monetary Unit To:");
        labelTo.setBounds(230,45,250,30);
        convertTo = new JComboBox<String>();
        convertTo.setBounds(230,70,150,30);
        for (Forex f : CurrencyConverter.forexList) {
        	convertTo.addItem(f.country);
        }

        from = new JLabel("Enter Amount to Convert:");
        from.setBounds(50,110,300,30);


        txtFrom = new JTextField(15);
        txtFrom.setBounds(50,140,300,30);


        compute = new JButton("Compute");
        compute.setBounds(100,250,100,30);
        add(compute);

        to = new JLabel("Total Amount Converted:");
        to.setBounds(50,170,300,30);


        txtTo = new JTextField(15);
        txtTo.setBounds(50,200,300,30);
        txtTo.setEditable(false);


        exit = new JButton("Exit");
        exit.setBounds(210,250,100,30);


        //register all the GUI components who will listen or invoke the methods
        convertFrom.addItemListener(this);
        convertTo.addItemListener(this);
        compute.addActionListener(this);
        txtFrom.addActionListener(this);
        exit.addActionListener(this);

            add(convertFrom);
            add(labelFrom);
            add(convertFrom);
            add(labelTo);
            add(convertTo);
            add(from);
            add(txtFrom);
            add(compute);
            add(to);
            add(txtTo);
            add(exit);

    }
    //the exit method
    public void exit(){
        setVisible(false);
        System.exit(0);
    }


    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==compute)
        {       

                input = Double.parseDouble(txtFrom.getText());
                String msg = txtFrom.getText();
        	String fromVal = convertFrom.getItemAt(convertFrom.getSelectedIndex());
        	String toVal = convertTo.getItemAt(convertTo.getSelectedIndex());
        	try {
				txtTo.setText(CurrencyConverter.convert(input, fromVal, toVal));
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }
        //this will asked the user to exit or not
        else
        {
            //this will display a confirm dialog box
            int choice = JOptionPane.showConfirmDialog(null,"Do you really want to quit?","Exit",JOptionPane.YES_NO_OPTION);
            if(choice==0) {
                curr.dispose();
                back.setVisible(true);
            }
        }
    }

    public void itemStateChanged(ItemEvent e)
    {

    }
}